//----------------------------------------------------------
// HUD_Game.cpp
//   Game-specific definition of abstract HUD class
// 
//----------------------------------------------------------

#include "HUD_Game.h"

HUD_Game::HUD_Game() 
{
	// initialize variables
	_timerShow = false;
	_fpsTimer = 0.0f;
	_fps = 0.0f;
}

HUD_Game::~HUD_Game() 
{
	// release any objects

	if (pFontTimer != NULL)
	{
		pFontTimer->Release();
		pFontTimer = NULL;
	}

	if (pFontFPS != NULL)
	{
		pFontFPS->Release();
		pFontFPS = NULL;
	}

	if (pTexMisses != NULL)
	{
		pTexMisses->Release();
		pTexMisses = NULL;
	}

	if (pSprite != NULL)
	{
		pSprite->Release();
		pSprite = NULL;
	}

}

bool HUD_Game::Initialize(LPDIRECT3DDEVICE9 pD3DDevice, Engine *pEngine, gamemode_t eMode) 
{
	_engine = pEngine;
	_pD3DDevice = pD3DDevice;
	_gamemode = eMode;

	// initialize for specific game mode
	InitFonts();
	InitTextures();
	CreateRects();

	return true;
}

bool HUD_Game::Shutdown() 
{
	// do nothing for now
	return true;
}

bool HUD_Game::InitFonts()
{
	// create the timer font
	if (FAILED (D3DXCreateFont(_pD3DDevice, 32, 0, FW_NORMAL, 0, FALSE, 
				DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, 
				DEFAULT_PITCH | FF_DONTCARE, "CatShop", &pFontTimer )))
	{
		return false;
	}

	// create the FPS font
	if (FAILED (D3DXCreateFont(_pD3DDevice, 20, 0, FW_NORMAL, 0, FALSE, 
				DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, 
				DEFAULT_PITCH | FF_DONTCARE, "CatShop", &pFontFPS )))
	{
		return false;
	}

	return true;
}

void HUD_Game::CreateRects()
{
	// create the timer rect
	_rTimer.left = 10;
	_rTimer.right = 100;
	_rTimer.top = 10;
	_rTimer.bottom = 70;

	// create the FPS rect
	_rFPS.left = 10;
	_rFPS.right = 100;
	_rFPS.top = 220;
	_rFPS.bottom = 270;

	// create the misses rect
	_rMisses.left = 400;
	_rMisses.right = 450;
	_rMisses.top = 400;
	_rMisses.bottom = 450;

}

void HUD_Game::DisplayText()
{
	float frameTime = _engine->GetFrameTime();

// FPS DISPLAY ________________________________
	// get current frame time
	_fpsTimer += frameTime;

	// only update FPS once a second
	if (_fpsTimer >= 1000.f)
	{
		// get FPS from the engine
		_fps = _engine->GetFPS();

		// Reset time for next loop
		_fpsTimer = 0.0f;
	}

	// draw everything

	sprintf(_cFPS, "%f", _fps);
	pFontFPS->DrawTextA(NULL, _cFPS, 8, &_rFPS, 0, 
		D3DCOLOR_ARGB(255, 255, 255, 255));

// TIMER DISPLAY ______________________________
	if (_timerShow)	// only show if timer enabled for the HUD
	{

		// update the countdown timer by elapsed engine time
		//   only when the timer is running and not finished
		if (_pTimer->isRunning() && (_pTimer->CurrentTime() > 0))
		{
			_pTimer->Update(frameTime);
		}

		// create the formatted time string to display
		std::string countdownLabel;
		char buffer[4];
		_itoa(_pTimer->Min(), buffer, 10);	// convert int to string
			countdownLabel += buffer;
			countdownLabel += ":";
		if (_pTimer->Sec() < 10) 
		{
			// add a leading zero if the secs are under 10
			countdownLabel += "0";
		}
		_itoa(_pTimer->Sec(), buffer, 10);	// convert int to string
			countdownLabel += buffer;

		// display it finally
		pFontTimer->DrawTextA(NULL, countdownLabel.c_str(), 4, &_rTimer, 0, 
			D3DCOLOR_ARGB(255, 255, 255, 255));

	}

}

bool HUD_Game::InitTextures()
{
	pSprite = NULL;
	pTexMisses = NULL;

	// create the misses left sprite
	if (FAILED (D3DXCreateSprite(_pD3DDevice, &pSprite)))
	{
		MessageBox(NULL, "Failed to create sprite object", "ERROR", MB_OK);
		return false;
	}

	// create the texture that will be draw as the sprite
	if (FAILED (D3DXCreateTextureFromFile(_pD3DDevice, 
		".\\Engine\\res\\red-circle-md_30.png", &pTexMisses )))
	{
		MessageBox(NULL, "Failed to create texture object", "ERROR", MB_OK);
		return false;
	}

	return true;
}

void HUD_Game::DisplayTextures()
{
	// TESTING FOR SPRITE/TEXTURE DISPLAY
	D3DXMATRIX scale;
	float xscale = 2.0f, yscale = 2.0f; // want texture 2x size
	// generate scaling matrix for sizing
	D3DXMatrixScaling(&scale, xscale, yscale, 1 );
	pSprite->SetTransform( &scale ); // set the scale for the texture
	D3DXVECTOR3 position( 200, 150, 0 );
	// compensate for scaling to maintain position where desired
	position.x /= xscale;
	position.y /= yscale;

	// draw the sprite
	pSprite->Begin(D3DXSPRITE_ALPHABLEND);
	pSprite->Draw(pTexMisses, NULL, NULL, &position, 0xFFFFFFFF);
	//pSprite->Draw(pTexMisses, &_rMisses, NULL, NULL, 0xFFFFFFFF);
	pSprite->End();
}

void HUD_Game::ShowTimer(bool timerShow) 
{
	// create the Timer if we're showing the timer
	//   otherwise kill it
	if (timerShow)
	{
		_pTimer = new Timer();
	}
	else
	{
		if (_pTimer != NULL) delete _pTimer;
	}

	_timerShow = timerShow;

	// eventually the Timer should be abstracted into 
	//  the Game Mode itself, leaving here for now
}